package com.mc.web.controller.lab;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.alibaba.fastjson.JSONObject;
import com.mc.clt.domain.CltInfo;
import com.mc.clt.domain.CltResult;
import com.mc.clt.service.ICltService;
import com.mc.common.annotation.Log;
import com.mc.common.constant.Constants;
import com.mc.common.core.controller.BaseController;
import com.mc.common.core.domain.AjaxResult;
import com.mc.common.core.domain.model.LoginUser;
import com.mc.common.core.page.TableDataInfo;
import com.mc.common.enums.BusinessType;
import com.mc.common.utils.DateUtils;
import com.mc.common.utils.ServletUtils;
import com.mc.framework.web.service.TokenService;
import com.mc.lab.domain.LabAssayGermplasm;
import com.mc.lab.domain.LabAuthenticate;
import com.mc.lab.domain.LabExpert;
import com.mc.lab.domain.LabGermplasmExprie;
import com.mc.lab.service.ILabAuthenticateService;
import com.mc.lab.service.ILabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.util.List;
import java.util.UUID;

@RestController
    @RequestMapping("/lab/clt")
public class LabController extends BaseController {
    @Autowired
    private ICltService cltService;
    @Autowired
    private ILabAuthenticateService labAuthenticateService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ILabService labService;



    /**
     * 查询采集信息
     * @param cltInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('lab:clt:list')")
    @GetMapping("/list")
    public TableDataInfo list(CltInfo cltInfo)
    {
        startPage();
        List<CltResult> list = cltService.selectCltResultListByCltInfo(cltInfo);
        return getDataTable(list);
    }
    /**
     * 上传鉴定信息
     * @param labAuthenticate
     * @return
     */
    @PreAuthorize("@ss.hasPermi('lab:authenticate:add')")
    @Log(title = "新增鉴定信息 ", businessType = BusinessType.INSERT)
    @PostMapping("/authenticate/add")
    public AjaxResult addAuthenticate(@RequestBody LabAuthenticate labAuthenticate)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        labAuthenticate.setCreateUserId(loginUser.getUsername());
        return toAjax(labAuthenticateService.insertLabAuthenticate(labAuthenticate));
    }
    /**
     * 查询鉴定信息
     * @param
     * @return
     */
    @PreAuthorize("@ss.hasPermi('lab:authenticate:query')")
    @GetMapping("/authenticate/{cltId}")
    public AjaxResult queryAuthenticate(@PathVariable("cltId") String cltId ){
        return AjaxResult.success(labAuthenticateService.selectLabAuthenticateListByCltID(cltId));
    }

    /**
     *
     * 到期提醒
     * @return
     */
    @PreAuthorize("@ss.hasPermi('lab:exprie:list')")
    @GetMapping("/exprie/list")
    public TableDataInfo exprieList(){
        startPage();
        List<LabGermplasmExprie> list =labService.selectLabGermplasmExprieList();
        return getDataTable(list);
    }
    /**
     * 修改种子活性到期
     */
    @PreAuthorize("@ss.hasPermi('lab:exprie:edit')")
    @Log(title = "种子活性到期 ", businessType = BusinessType.UPDATE)
    @PutMapping("/exprie")
    public AjaxResult edit(@RequestBody LabGermplasmExprie labGermplasmExprie)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        labGermplasmExprie.setUpdateBy(loginUser.getUsername());
        labGermplasmExprie.setUpdateTime(DateUtils.getNowDate());
        return toAjax(labService.updateLabGermplasmExprie(labGermplasmExprie));
    }

    /**
     * 查询种子化验 列表
     */
    @PreAuthorize("@ss.hasPermi('lab:germplasm:list')")
    @GetMapping("/germplasm/list")
    public TableDataInfo germplasmList(String cltId)
    {
        startPage();
        List<LabAssayGermplasm> list = labService.selectLabAssayGermplasmByCltId(cltId);
        return getDataTable(list);
    }
    /**
     * 获取种子化验 详细信息
     */
    @PreAuthorize("@ss.hasPermi('lab:germplasm:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(labService.selectLabAssayGermplasm(id));
    }
    /**
     * 新增种子化验
     */
    @PreAuthorize("@ss.hasPermi('lab:germplasm:add')")
    @Log(title = "新增种子化验 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LabAssayGermplasm labAssayGermplasm)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        labAssayGermplasm.setCreateUserId(loginUser.getUsername());
        labAssayGermplasm.setCreateTime(DateUtils.getNowDate());
        return toAjax(labService.insertLabAssayGermplasm(labAssayGermplasm));
    }

    /**
     * 修改种子化验
     */
    @PreAuthorize("@ss.hasPermi('lab:germplasm:edit')")
    @Log(title = "修改种子化验 ", businessType = BusinessType.UPDATE)
    @PutMapping("/germplasm")
    public AjaxResult edit(@RequestBody LabAssayGermplasm labAssayGermplasm)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        labAssayGermplasm.setUpdateUserId(loginUser.getUsername());
        labAssayGermplasm.setUpdateTime(DateUtils.getNowDate());
        return toAjax(labService.updateLabAssayGermplasm(labAssayGermplasm));
    }
    @GetMapping("/qrCode")
    public AjaxResult getQRCode() {
        QrConfig config = new QrConfig(300, 300);
        // 设置边距，既二维码和背景之间的边距
//        config.setMargin(3);
//        // 设置前景色，既二维码颜色（绿色）
//        config.setForeColor(Color.BLUE);
//        // 设置背景色（灰色）
//        config.setBackColor(Color.GRAY);
//        config.setImg("E:\\mc\\photo\\logo\\favicon.png");
        String uuid  = UUID.randomUUID().toString().replaceAll("-","");
        QrCodeUtil.generate(Constants.QR_CODE_URL+"",
                //附带logo
                //QrConfig.create().setImg("E:\\mc\\photo\\logo\\logo.png"),
                config,
                FileUtil.file(Constants.QR_CODE_PATH+"qrcode.jpg"));
        return AjaxResult.success("qrcode.jpg");

        //return "生成成功";
    }

}
